package xyz.mkkbjb.http;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.CharArrayBuffer;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import xyz.mkkbjb.pojo.TuLingAiAnswer;
import xyz.mkkbjb.pojo.UserInfo;
 
public class HttpRobot {
 
	private static String ROBOT_URL = "http://openapi.tuling123.com/openapi/api/v2";
	/**
	 * post请求，参数为json字符串
	 * 
	 * @param url        请求地址
	 * @param jsonString json字符串
	 * @return 响应
	 */
	public static TuLingAiAnswer postJson(String url, String jsonString) {
 
		String result = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost post = new HttpPost(url);
		CloseableHttpResponse response = null;
		TuLingAiAnswer aiAnswer = null;
		try {
			post.setEntity(new ByteArrayEntity(jsonString.getBytes("UTF-8")));
			response = httpClient.execute(post);
			if (response != null && response.getStatusLine().getStatusCode() == 200) {
				HttpEntity entity = response.getEntity();
				result = entityToString(entity);
				JSONObject jsonObject = JSONObject.fromObject(result); // 将字符串转化为JSONObject类型
				/*
				 * 分析 JSON的中的数据的格式，提取出来 System.out.println("jsonObject --->" + jsonObject);
				 * jsonObject ---> {"intent":{"actionName":"","code":7110,"intentName":""},
				 * "results":[{"groupType":0,"resultType":"text","values":{"text":"你的小可爱做错了什么吗？"
				 * }}]}
				 * 
				 */
				JSONArray ja = jsonObject.getJSONArray("results");
				System.out.println("ja ---> " + ja);
				JSONObject jo = ja.getJSONObject(ja.size() - 1); // 提取出最后一个 text类型的数据-
				JSONObject jsonObject1 = jo.getJSONObject("values");
				result = jsonObject1.getString("text"); // 去除 value 中的 text数据
				JSONObject code = jsonObject.getJSONObject("intent");
				System.out.println("ja ---> " + code);
				String string = code.getString("code");
				int answerCode = 200;
				if (string != null) {
					answerCode = Integer.parseInt(string);
				}
				aiAnswer = new TuLingAiAnswer();
				aiAnswer.setAnswer(result);
				aiAnswer.setAnswerCode(answerCode);
			}
 
			return aiAnswer;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpClient.close();
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
 
	private static String entityToString(HttpEntity entity) throws IOException {
		String result = null;
		if (entity != null) {
			long lenth = entity.getContentLength();
			if (lenth != -1 && lenth < 2048) {
				result = EntityUtils.toString(entity, "UTF-8");
			} else {
				InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
				CharArrayBuffer buffer = new CharArrayBuffer(2048);
				char[] tmp = new char[1024];
				int l;
				while ((l = reader1.read(tmp)) != -1) {
					buffer.append(tmp, 0, l);
				}
				result = buffer.toString();
			}
		}
		return result;
	}
 
	public static JSONObject transJosn(String text,Integer reqType,UserInfo userIn) { // 构造JSON请求参数
		JSONObject jsonObject = new JSONObject();
		JSONObject perception = new JSONObject();
		if (reqType == 1) {
			jsonObject.put("reqType", "1");
			
			JSONObject inputImage = new JSONObject();
			inputImage.put("url", text);
			perception.put("inputImage", inputImage);
		}else {
			jsonObject.put("reqType", "0");
			
			JSONObject inputText = new JSONObject();
			inputText.put("text", text);
			perception.put("inputText", inputText);
		}
		jsonObject.put("perception", perception);
 
		JSONObject userInfo = new JSONObject();
		userInfo.put("apiKey", userIn.getKey());
		userInfo.put("userId", userIn.getUserId());
		jsonObject.put("userInfo", userInfo);
 
		return jsonObject;
	}
	public static void main(String[] args) {
		String content = "厉害了";
		UserInfo userInfo = new UserInfo();
		userInfo.setKey("yourkey");
		userInfo.setUserId("fuchen");
		JSONObject jsonObject = HttpRobot.transJosn(content,0,userInfo); // content是文本消息
//		String content = "https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/w%3D268%3Bg%3D0/sign=0003b03088b1cb133e693b15e56f3173/0bd162d9f2d3572c257447038f13632763d0c35f.jpg";
//		JSONObject jsonObject = HttpRobot.transJosn(content,1); // content是文本消息
		TuLingAiAnswer js = HttpRobot.postJson(ROBOT_URL , jsonObject.toString());
		System.err.println(js.getAnswer()+"--"+js.getAnswerCode());
	}
}